﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace hdbankwebservice
{
    public class MineUtils
    {
        public static string szSecureKey = "12345678";

        public static DESCryptoServiceProvider DESalg = new DESCryptoServiceProvider();

        public static string ByteArrayToString(byte[] bytes)
        {
            StringBuilder hex = new StringBuilder(bytes.Length * 2);
            foreach (byte b in bytes)
            {
                hex.AppendFormat("{0:x2}", b);
            }
            return hex.ToString();
        }

        public static byte[] StringToByteArray(String hexString)
        {
            int numberChars = hexString.Length;
            byte[] bytes = new byte[numberChars / 2];
            for (int i = 0; i < numberChars; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
            }
            return bytes;
        }

        public static string SIDGenerate()
        {
            DateTime currentDate = DateTime.Now;
            byte[] encryptedByteArray = MaHoa(currentDate.ToString());
            string SID = ByteArrayToString(encryptedByteArray);
            return SID;
        }

        public static byte[] MaHoa(string Data)
        {
            try
            {
                DESalg.Key = ASCIIEncoding.ASCII.GetBytes(szSecureKey);
                DESalg.IV = ASCIIEncoding.ASCII.GetBytes(szSecureKey);
                MemoryStream mStream = new MemoryStream();

                CryptoStream cStream = new CryptoStream(mStream,
                    new DESCryptoServiceProvider().CreateEncryptor(DESalg.Key, DESalg.IV),
                    CryptoStreamMode.Write);

                byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data);

                cStream.Write(toEncrypt, 0, toEncrypt.Length);
                cStream.FlushFinalBlock();

                byte[] ret = mStream.ToArray();

                cStream.Close();
                mStream.Close();

                return ret;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine("Loi roi: {0}", e.Message);
                return null;
            }

        }

        public static string GiaiMa(byte[] Data)
        {
            try
            {
                DESalg.Key = ASCIIEncoding.ASCII.GetBytes(szSecureKey);
                DESalg.IV = ASCIIEncoding.ASCII.GetBytes(szSecureKey);

                MemoryStream msDecrypt = new MemoryStream(Data);

                CryptoStream csDecrypt = new CryptoStream(msDecrypt,
                    new DESCryptoServiceProvider().CreateDecryptor(DESalg.Key, DESalg.IV),
                    CryptoStreamMode.Read);

                byte[] fromEncrypt = new byte[Data.Length];

                csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
                string ketqua = new ASCIIEncoding().GetString(fromEncrypt);
                return ketqua.Substring(0,ketqua.IndexOf('\0'));
             //   return ketqua;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine("Loi roi: {0}", e.Message);
                return null;
            }
        }

        public static string HashPass(string userName, string password)
        {
            return Hash.getSHA1Hash(password + userName[0] + userName[userName.Length - 1]);
        }
    }
}